<template>
  <div>
    <goback></goback>
    <van-address-edit
      :address-info="con"
      :area-list="areaList"
      show-set-default
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      area-placeholder
      @save="onSave"
    >
      <div class="line van-cell van-field">
        <span class="van-cell__title van-field__label">标签</span>
        <van-radio-group v-model="a" class="van-cell__value van-field__value">
          <van-radio name="家">家</van-radio>
          <van-radio name="公司">公司</van-radio>
          <van-radio name="学校">学校</van-radio>
        </van-radio-group>
      </div>
    </van-address-edit>>
    <!-- <van-area title="标题" :area-list="areaList" /> -->
  </div>
</template>
<script>
import { Toast } from "vant";
import { areaList } from "@vant/area-data";
import { reqaddresssadd, reqAddressList, reqaddressedit } from "../../http/api";
export default {

  data() {
    return {
      areaList,
      a: "家",
      con: {},
    };
  },
  mounted() {
    //修改渲染
    if (this.$route.query.id) {
      reqAddressList({
        uid: JSON.parse(localStorage.getItem("user")).uid,
        id: this.$route.query.id,
      }).then((res) => {
        // console.log(res);
        let con = res.data.list[0];
        this.con = {
          name: con.username,
          tel: con.userphone,
          address: con.location,
          areaCode: con.regioncode,
          addressDetail: con.useraddress,
          isDefault: con.status == 1,
        };
        this.a = con.label;
      });
    }
  },
  methods: {
    onSave(ev) {
      if (this.$route.query.id) {
        reqaddressedit({
          username: ev.name,
          userphone: ev.tel,
          regioncode: ev.areaCode,
          location: ev.province + ev.city + ev.county,
          useraddress: ev.addressDetail,
          status: ev.isDefault ? 1 : 0,
          label: this.a,
          id:this.$route.query.id
        }).then((res) => {
          if (res.data.code == 200) {
            Toast(res.data.msg);
            this.$router.replace("/address");
          }
        });
      } else {
        //添加地址
        console.log(ev);
        reqaddresssadd({
          uid: JSON.parse(localStorage.getItem("user")).uid,
          username: ev.name,
          userphone: ev.tel,
          regioncode: ev.areaCode,
          location: ev.province + ev.city + ev.county,
          useraddress: ev.addressDetail,
          status: ev.isDefault ? 1 : 0,
          label: this.a,
        }).then((res) => {
          if (res.data.code == 200) {
            Toast(res.data.msg);
            this.$router.replace("/address");
          }
        });
      }
    },
    onDelete() {
      Toast("delete");
    },
  },
};
</script>

<style scoped>
.van-radio-group {
  overflow: hidden;
}

.van-radio {
  float: left;
  margin-left: 0.5rem;
}
.van-radio:nth-child(1) {
  margin-left: 0;
}
</style>